<?php

namespace Module;

class CommentsModule extends BaseModule {

    /**
     *
     */
    public function listAction() {
        $args = \Lib\Registry::get('args');
        $objectId = (int) $args['Id'];
        
        $comments = \Lib\Model\CommentModel::getListByObjectId($objectId);
        if(!$comments){
          $comments = array();
        }
        return $this->showTemplate(__FUNCTION__, array('comments'=> $comments, 'objectId' => $objectId), '');
    }

    public function addAction() {
        $args = \Lib\Registry::get('args');
        $comment = array();
        $comment['username'] = \Lib\Model\UserModel::getUser();
        $comment['comment'] = $_POST['comment'];
        $comment['level'] = (isset($_POST['level']) ? $_POST['level'] : 0) + 1;
        $comment['date'] = date("Y-m-d H:i:s");
        $objectId = (int) $args['objectId'];
        $parentId = $_POST['parent'];
        $user = \Lib\Model\UserModel::getByName($comment['username']);
        if($comment['id'] = \Lib\Model\CommentModel::add($comment['comment'], $objectId, $user['id'], $parentId)){
            $status = TRUE;
        } else {
            $status = FALSE;
        }
        if(!AJAX){
            header("Location: {$_SERVER['HTTP_REFERER']}#comment-".$comment['id']);
        }
        return $this->showTemplate(__FUNCTION__, array(
            'parentId'=> $parentId, 
            'objectId'=> $objectId,
            'status'=> $status,
            'comment' => $comment
            ), '');
    }
    
    public function addFormAction() {
        $args = \Lib\Registry::get('args');
        $parentId = (int) $args['parentId'];
        $objectId = (int) $args['objectId'];
        return $this->showTemplate(__FUNCTION__, array('parentId'=> $parentId, 'objectId'=> $objectId), '');
    }

}

?>
